package com.itv.email.data;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.core.annotation.Order;

import java.io.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * User: bjxiajun
 * Date: 14-5-21
 * Time: 上午9:49
 * 处理用户浏览的数据
 */
public class BrowseDataProcess {
    private int minCount;

    /**
     * 主函数
     *
     * @param args args[0] 文件存放地址,args[1] 每个用户最少浏览一个分类的次数
     */
    /*public static void main(String[] args) {
        if (args.length < 2) {
            throw new IllegalArgumentException("需要2个参数：1,文件存放地址。2,每个用户最少浏览一个分类的次数");
        }
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:ItvSpider.xml");
        DubboUserInfoService userInfoService = (DubboUserInfoService) context.getBean("dubboUserInfoService");
        BrowseDataProcess bdp = new BrowseDataProcess();
        bdp.minCount = Integer.parseInt(args[1]);
        try {
            List<String> res=bdp.process(args[0]);
            String filePath=args.length>=3?args[2]:null;
            bdp.writeData(res,filePath,userInfoService);
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            System.exit(0);
        }
    }

    *//**
     * 处理浏览数据
     * @param filePath
     * @return
     * @throws IOException
     *//*
    public List<String> process(String filePath) throws IOException {
        File file = new File(filePath);
        if (!file.exists()) {
            throw new IOException("文件[" + filePath + "]未找到。");
        }
        InputStream is = new FileInputStream(file);
        InputStreamReader isr = new InputStreamReader(is, "utf-8");
        BufferedReader br = new BufferedReader(isr);
        String line;
        String user = null;
        Map<String, String> c3_info = new HashMap<String, String>();
        Map<String, Integer> c3_count = new HashMap<String, Integer>();
        List<String> res = new ArrayList<String>(10000);
        while ((line = br.readLine()) != null) {
            String[] info = line.split("\t");
            if (user == null) {
                user = info[0];
            } else if (!user.equals(info[0])) {
                String c3 = filterCount(c3_count);
                if (c3 != null) {
                    res.add(c3_info.get(c3));
                }
                c3_info.clear();
                c3_count.clear();
                user = info[0];
            }
            String newInfo = info[0] + "\t" + info[2] + "\t" + info[3] + "\t" + info[4] + "\t" + info[5];
            c3_info.put(info[4], newInfo);
            if(c3_count.get(info[4]) == null||c3_count.get(info[4]) == 0){
                c3_count.put(info[4], 1);
            }else{
                int c=c3_count.get(info[4]) + 1;
                c3_count.put(info[4], c);
            }
        }
        return res;
    }

    *//**
     * 过滤浏览分类次数
     * @param c3_count
     * @return
     *//*
    public String filterCount(Map<String, Integer> c3_count) {
        String c3 = null;
        int count = 0;
        for (Map.Entry<String, Integer> entry : c3_count.entrySet()) {
            if (entry.getValue() > count) {
                c3 = entry.getKey();
                count = entry.getValue();
            }
        }
        if (count >= minCount) {
            return c3;
        }
        return null;
    }

    *//**
     * 保存文件
     * @param list
     * @param outPath
     * @throws IOException
     *//*
    private String writeData(List<String> list,String outPath,DubboUserInfoService userInfoService) throws
            IOException {
        String filePath;
        if(outPath==null||"".equals(outPath)){
            filePath= "order.txt";
        }else{
            filePath=outPath;
        }
        File file = new File(filePath);
        if (list == null || list.size() <= 0) {
            return file.getPath();
        }
        if (!file.exists()) {
            file.createNewFile();
        }
        OutputStream in = new FileOutputStream(file);
        OutputStreamWriter isr = new OutputStreamWriter(in, "utf-8");
        BufferedWriter writer = new BufferedWriter(isr,51200);
        int emailCount=0;
        for (int i = 0; i < list.size(); i++) {
            String userInfo=list.get(i);
            String em=getEmail(userInfoService,userInfo.split("\t")[0]);
            if(em==null){
               continue;
            }
            userInfo=userInfo+"\t"+em;
            writer.write(userInfo);
            writer.newLine();
            emailCount++;
            if(emailCount>=16000){
                break;
            }
        }
        writer.close();
        isr.close();
        in.close();
        return file.getPath();
    }
    public String getEmail(DubboUserInfoService userInfoService,String pin){
        try {
            UserBaseInfoVO user = userInfoService.getJiceUserInfoBasicByPin(pin);
            Thread.sleep(50);
            if(user!=null&&user.getEmail()!=null&&!"".equals(user.getEmail())){
                return user.getEmail();
            }
        } catch (Exception e) {
            System.out.println("----------------------获取email出错--------");
        }
        return null;
    }*/
}
